from django.forms.models import model_to_dict
from django.http import JsonResponse

from fairy_server import models


# Create your views here.

# 随机查询一个汉字
def hz_random(request):
    data = models.ChineseCharacter.objects.order_by('?')[:1]

    dataResponse = []
    for single in data:
        single = model_to_dict(single)
        dataResponse.append(single)
    return JsonResponse(dataResponse, safe=False)

# 通过名字搜索汉字
def hz_search(request, name):
    data = models.ChineseCharacter.objects.filter(name=name)

    dataResponse = []
    for single in data:
        single = model_to_dict(single)
        dataResponse.append(single)
    return JsonResponse(dataResponse, safe=False)